package both;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import server.GameShowServer;

/**
 * Maintains the list of teams and contestants.
 * 
 * @author Timothy Woodford
 */

public class Roster{

    private ArrayList<Team> teams = new ArrayList<Team>();

    private Team getTeam(String teamName) {

        Team ret = null;

        for(Team team: teams) {

            if(team.getName().equals(teamName)) {

                ret = team;

            }

        }

        return ret;

    }

    

    public void addContestant(Contestant contestant) {

        Team tg = getTeam(contestant.getTeamName());

        if(tg==null) {

            tg = new Team(contestant.getTeamName());

            teams.add(tg);

        }
        tg.addMember(contestant);
        //GameShowServer.tickContestants();
        //System.out.println("con++");
        //System.out.println(GameShowServer.getNumContestants());
    }

    

    public void removeContestant(Contestant contestant) {

        Team tg = getTeam(contestant.getTeamName());

        if(tg!=null) {

            tg.removeMember(contestant);

        }

        trimTeams();

    }

    

    private void trimTeams() {

        ArrayList<Team> newTeams = new ArrayList<Team>(teams.size());

        for(Team existing: teams) {

            if (existing.getTeamMembers().size()>0) {

                newTeams.add(existing);

            }

        }

        teams = newTeams;

    }

    

    private static class TeamComparator implements Comparator<Team> {


        @Override

        public int compare(Team t1, Team t2) {

            return t1.getName().compareTo(t2.getName());

        }

        

    }

    

    private static class PercentComparator implements Comparator<Team>{


        @Override

        public int compare(Team t1, Team t2) {

            if(t1.percentageCorrect()>t2.percentageCorrect())

                return 1;

            else if(t1.percentageCorrect()==t2.percentageCorrect())

                return 0;

            else

                return -1;

        }

        

    }

    /**

     * Get a list of teams, sorted by score

     * @return

     */

    public ArrayList<Team> teamsByScore() {

        ArrayList<Team> ret = new ArrayList<Team>(teams);

        Collections.sort(ret, new TeamComparator());

        return ret;

    }

    

    public ArrayList<Team> teamsByPercentage(){

        ArrayList<Team> ret=new ArrayList<Team>(teams);

        Collections.sort(ret,new PercentComparator());

        return ret;

    }

    

    /**

     * Get a list of contestants, sorted by team first, the name

     * @return

     */

    public ArrayList<Contestant> contestantsByTeamName() {

        ArrayList<Contestant> contestants = new ArrayList<Contestant>();

        for(Team team: teams) {

            contestants.addAll(team.getTeamMembers());

        }

        Collections.sort(contestants, new ContestantComparator()); // This does a TimSort (that's actually an algorithm)

        return contestants;

    }

    

    public void setTeams(ArrayList<Team> teams){

        this.teams=teams;

    }

    

    public Contestant getContestantByID(String ID){

        for(Contestant contestant:contestantsByTeamName())

            if(contestant.getUniqueID().equals(ID))

                return contestant;

        return null;

    }

    

    public Team getTeamByName(String teamName){

        for(Team team:teamsByScore())

            if(team.getName().equals(teamName))

                return team;

        return null;

    }

    

    private static class ContestantComparator implements Comparator<Contestant> {


        @Override

        public int compare(Contestant c1, Contestant c2) {

            int ret;

            if((ret=c1.getTeamName().compareTo(c2.getTeamName()))==0) {

                ret=c1.getTeamName().compareTo(c2.getTeamName());

            }

            return ret;

        }

    }
}




